package com.zhiyou.app.shop.website.action;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.zhiyou.app.shop.common.BaseAction;
import com.zhiyou.app.shop.common.util.ShopConstants;
import com.zhiyou.app.shop.website.dao.pojo.DicItem;
import com.zhiyou.app.shop.website.dao.pojo.User;
import com.zhiyou.app.shop.website.service.IDicItemService;
import com.zhiyou.app.shop.website.service.INewsService;
import com.zhiyou.app.shop.website.service.IStockService;
import com.zhiyou.app.shop.website.service.IUserManagerService;

@SuppressWarnings("serial")
public class LoginAction extends BaseAction {
	
	@Autowired
	private IUserManagerService userManagerService;
	@Autowired
	private INewsService newsService;
	@Autowired
	private IDicItemService dicItemService;
	@Autowired
	private IStockService stockService;
	
	private String r; //随机参数，刷新验证码
	private String code;
	private String username;
	private String password;
	private String loginMsg;
	private String loginReturn;
	
	private float prize; //股票价格
	
	private List<DicItem> levels; //用户级别的数据字典
	/**
	 * 判断输入的验证码
	 * @return
	 */
	public String check(){
		String imageRand = (String) getHttpSession().getAttribute("rand");
		if(!imageRand.equals(code)){
			try {
				//getResponse().getWriter().print("验证码输入错误");
				getResponse().setContentType("text/html;charset=UTF-8");
				getResponse().getWriter().println("<script>alert('验证码输入错误');</script>");  
				//getResponse().getWriter().println("<script>window.history.back();</script>");  
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	/*
	 * 显示验证码图片
	 */
	public String image(){
		return SUCCESS;
	}
	
	/**
	 * 用户登录
	 * @return
	 */
	public String login(){
		loginReturn = "login-failure";
		if (username != null && password!= null) {
			User user = new User();
			user.setUserAccount(username);
			user.setFirstPwd(encodePassword(password));
			user.setFlag(ShopConstants.TRUE);
			user.setActivity(ShopConstants.TRUE);
			// 根据用户名和密码获取用户列表
			try {
				List<User> list = userManagerService.login(user);
				if (list != null && list.size() > 0) {
					String imageRand = (String) getHttpSession().getAttribute("rand");
					if(imageRand != null && (!imageRand.equals(code))){
						loginMsg = "验证码错误!";
					}else{
						getSession().put(ShopConstants.LOGIN_USER, list.get(0));
						levels = dicItemService.findByType(ShopConstants.LEVEL);
						pageSize = 8;
						newsService.getNews(getInitedPage());
						prize = stockService.findPrice();
						loginReturn = SUCCESS;
					}
				} else {
					loginMsg = "帐号或者密码错误!";
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else {
			loginMsg = "帐号或者密码不可以为空!";
		}
		return loginReturn;
	}
	
	/**
	 * 用户登出系统
	 */
	public String loginOut(){
		getSession().remove(ShopConstants.LOGIN_USER);
		return SUCCESS;
	}
	
	
	/******************* setter and getter *********************/

	
	public String getR() {
		return r;
	}
	public void setR(String r) {
		this.r = r;
	}
	
	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getLoginMsg() {
		loginMsg = getText(loginMsg);
		return loginMsg;
	}

	public float getPrize() {
		return prize;
	}

	public void setPrize(float prize) {
		this.prize = prize;
	}

	
}
